package com.jsmile.cloud.authc.sys.controller;

import java.util.List;

import javax.validation.Valid;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jsmile.cloud.authc.api.req.sys.ReqSysDeptPageQuery;
import com.jsmile.cloud.authc.api.req.sys.ReqSysDeptQuery;
import com.jsmile.cloud.authc.api.req.sys.ReqSysDeptUpdate;
import com.jsmile.cloud.authc.api.res.ResSysDept;
import com.jsmile.cloud.authc.sys.SysDeptApplicationService;
import com.jsmile.mail.security.controller.AuthcController;
import com.jsmile.mail.security.handler.JsmileContext;
import com.jsmile.mall.api.JSmileResult;
import com.jsmile.mall.api.annotation.CurrentContext;
import com.jsmile.mall.api.constants.JSmileConstant;
import com.jsmile.mall.api.enums.LogTypeEnum;
import com.jsmile.mall.api.to.JsmilePage;
import com.jsmile.mall.log.annotation.JsmileLog;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * @author 龚亮
 * @version 1.0
 * @desc 控制器
 * @date 2018年08月25日 04:57:24
 */
@RestController
@Api(tags = "部门管理")
@Slf4j
@AllArgsConstructor
@RequestMapping("/sys/dept")
public class SysDeptController extends AuthcController {

    private final SysDeptApplicationService sysDeptApplicationService;

    @RequestMapping(value = "/findAll", method = RequestMethod.POST)
    public JSmileResult<JsmilePage<ResSysDept>> getPageList(@RequestBody @Valid ReqSysDeptPageQuery reqSysDeptPageQuery, @CurrentContext JsmileContext jsmileContext) {
        reqSysDeptPageQuery.setCurTenantId(jsmileContext.getTenantId());
        IPage<ResSysDept> pages = sysDeptApplicationService.getPageList(reqSysDeptPageQuery);
        return JSmileResult.success(new JsmilePage(reqSysDeptPageQuery.getPageIndex(), reqSysDeptPageQuery.getPageSize(), pages.getTotal(), pages.getRecords()));
    }

    @RequestMapping(value = "/getList", method = RequestMethod.POST)
    public JSmileResult<JsmilePage<ResSysDept>> getPageList(@RequestBody @Valid ReqSysDeptQuery reqSysDeptQuery, @CurrentContext JsmileContext jsmileContext) {
        reqSysDeptQuery.setCurTenantId(jsmileContext.getTenantId());
        List<ResSysDept> pages = sysDeptApplicationService.getList(reqSysDeptQuery);
        return JSmileResult.success(pages);
    }

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ApiOperation(value = "新增部门")
    @JsmileLog(title = "用户中心->新增部门", type = LogTypeEnum.INSERT, systemId = JSmileConstant.SystemId.AUTHC)
    public JSmileResult saveDict(@RequestBody @Valid ReqSysDeptUpdate reqSysDeptUpdate, @CurrentContext JsmileContext jsmileContext) {
        this.initContext(jsmileContext, JSmileConstant.SystemId.AUTHC);
        reqSysDeptUpdate.setTenantId(jsmileContext.getTenantId());
        reqSysDeptUpdate.setCreateBy(jsmileContext.getUserName());
        reqSysDeptUpdate.setUpdateBy(jsmileContext.getUserName());
        if (!sysDeptApplicationService.saveDept(reqSysDeptUpdate)) {
            return JSmileResult.error("添加部门异常!");
        }
        return JSmileResult.success();
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ApiOperation(value = "修改部门")
    @JsmileLog(title = "用户中心->修改部门", type = LogTypeEnum.UPDATE, systemId = JSmileConstant.SystemId.AUTHC)
    public JSmileResult updateDict(@RequestBody @Valid ReqSysDeptUpdate reqSysDeptUpdate, @CurrentContext JsmileContext jsmileContext) {
        this.initContext(jsmileContext, JSmileConstant.SystemId.AUTHC);
        reqSysDeptUpdate.setTenantId(jsmileContext.getTenantId());
        reqSysDeptUpdate.setCreateBy(jsmileContext.getUserName());
        reqSysDeptUpdate.setUpdateBy(jsmileContext.getUserName());
        if (!sysDeptApplicationService.updateDept(reqSysDeptUpdate)) {
            return JSmileResult.error("修改部门异常!");
        }
        return JSmileResult.success();
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ApiOperation(value = "删除部门")
    @JsmileLog(title = "用户中心->删除部门", type = LogTypeEnum.DELETE, systemId = JSmileConstant.SystemId.AUTHC)
    public JSmileResult deleteDict(@RequestBody @Valid ReqSysDeptUpdate reqSysDeptUpdate, @CurrentContext JsmileContext jsmileContext) {
        this.initContext(jsmileContext, JSmileConstant.SystemId.AUTHC);
        reqSysDeptUpdate.setTenantId(jsmileContext.getTenantId());
        if (!sysDeptApplicationService.deleteDept(reqSysDeptUpdate)) {
            return JSmileResult.error("删除部门异常!");
        }
        return JSmileResult.success();
    }
}
